package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.wemedia.WmUserControllerApi;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    WmUserService wmUserService;

    /**
     * 保存自媒体用户
     * @param wmUser
     * @return wmUser 需要主键返回
     */
    @PostMapping("/save")
    @Override
    public WmUser save(@RequestBody WmUser wmUser) {

        if (wmUserService.save(wmUser)) {
            return wmUser;
        }

        return null;
    }

    /**
     * 根据登录名查询当前用户
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {
        return  wmUserService.getOne(Wrappers.<WmUser>lambdaQuery()
                .eq(WmUser::getName, name)
        );
    }

//======================1/17
    /**
     * 根据id查询自媒体用户
     * @param id
     * @return
     */
    @GetMapping("/findOne/{id}")
    @Override
    public WmUser findWmUserById(@PathVariable("id") Integer id) {
        return wmUserService.getById(id);
    }
}
